package com.jyw.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jyw.R;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/10
 */

public class TimePickerVAdapter extends RecyclerView.Adapter<TimePickerVAdapter.TimePickerVHolder> {
    TimeWraper[] dataStr = {new TimeWraper("30分钟之上门", 24)
            , new TimeWraper("6:00-7:00", 6)
            , new TimeWraper("7:00-8:00", 7)
            , new TimeWraper("8:00-9:00", 8)
            , new TimeWraper("9:00-10:00", 9)
            , new TimeWraper("10:00-11:00", 10)
            , new TimeWraper("11:00-12:00", 11)
            , new TimeWraper("12:00-13:00", 12)
            , new TimeWraper("13:00-14:00", 13)
            , new TimeWraper("14:00-15:00", 14)
            , new TimeWraper("15:00-16:00", 15)
            , new TimeWraper("16:00-17:00", 16)
            , new TimeWraper("17:00-18:00", 17)
            , new TimeWraper("18:00-19:00", 18)
            , new TimeWraper("19:00-20:00", 19)
            , new TimeWraper("20:00-21:00", 20)
            , new TimeWraper("21:00-22:00", 21)};
    private Context mCtx;
    private Calendar sCalendar;
    private Map<TimeWraper, Boolean> data;
    private int selectedPosition = 100;


    public TimePickerVAdapter(Context ctc) {
        this.mCtx = ctc;
        data = new HashMap<>();
        sCalendar = Calendar.getInstance(Locale.CHINA);
        initData();
    }

    private void initData() {
        int hourNow = sCalendar.get(Calendar.HOUR_OF_DAY);
        if (hourNow < 6 || hourNow > 22) {
            for (int i = 0; i < dataStr.length; i++) {
                data.put(dataStr[i], Boolean.FALSE);
            }
        } else {
            for (int i = 0; i < dataStr.length; i++) {
                if (dataStr[i].hourBegin < hourNow) {
                    data.put(dataStr[i], Boolean.TRUE);
                } else {
                    data.put(dataStr[i], Boolean.FALSE);
                }
            }
        }
    }


    private class TimeWraper {
        String txt;
        int hourBegin;

        TimeWraper(String txt, int hourBegin) {
            this.txt = txt;
            this.hourBegin = hourBegin;
        }
    }

    public void swapToToDay(boolean isit) {
        if (isit) {
            sCalendar.setTime(new Date());
            data.clear();
            initData();
            selectedPosition = 100;
            notifyDataSetChanged();

        } else {
            sCalendar.set(Calendar.HOUR_OF_DAY, 0);
            data.clear();
            initData();
            selectedPosition = 100;
            notifyDataSetChanged();
        }
    }

    public String getFinalTime() {
        if (selectedPosition == 100) {
            return "尚未选择";
        } else {
            return dataStr[selectedPosition].txt;
        }
    }

    @Override
    public TimePickerVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TimePickerVHolder(LayoutInflater.from(mCtx).inflate(R.layout.item_time_picker_v, null));
    }

    @Override
    public void onBindViewHolder(final TimePickerVHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.tv.setText(dataStr[position].txt);
        if (data.get(dataStr[position])) {
            holder.tv.setBackgroundColor(mCtx.getResources().getColor(R.color.border_gray_alpha));
            holder.tv.setClickable(false);
        } else {
            holder.tv.setBackgroundColor(Color.WHITE);
            holder.tv.setClickable(true);
            if (position == selectedPosition) {
                holder.tv.setBackgroundColor(mCtx.getResources().getColor(R.color.yellow));
            }
        }

        holder.tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = position;
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataStr.length;
    }

    class TimePickerVHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        TimePickerVHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
